package com.bkhech.netty.httpwebsocket;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.bkhech.netty.httpwebsocket.pojo.*;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.lang.reflect.Type;
import java.time.LocalDateTime;

/**
 *
 * 解决JDK module UNNAMED
 *  1. 启动参数需要加上--add-exports java.base/jdk.internal.access=ALL-UNNAMED
 *  pom.xml需要配置编译参数--add-exports=java.base/jdk.internal.access=ALL-UNNAMED，不然maven编译会报错
 *  2. 使用兼容的Netty版本，Netty 4.1.115.Final 修复了这个问题
 *
 * @author guowm
 * @date 2024/03/30
 *
 */
public class Main {
    public static void main(String[] args) {
        for (int i = 1; i <= 5; i++) {
            System.out.println("i = " + i);
        }


        ChatMessage chatMessage = ChatMessage.builder()
                .msgId("1")
                .senderId("1")
                .receiverId("2")
                .chatType(ChatType.WORDS)
                .msg("你好")
                .build();
        Request<Message> request = Request.builder().msgType(MsgType.CHAT).message(chatMessage).build();
//        ChatMessage chatMessage = new ChatMessage();
//        chatMessage.setMsgId("1");
//        chatMessage.setSenderId("1");
//        chatMessage.setReceiverId("2");
//        chatMessage.setChatType(ChatType.WORDS);
//        chatMessage.setMsg("你好");
//
//        Request<ChatMessage> request = new Request();
//        request.setMsgType(MsgType.CHAT);
//        request.setMessage(chatMessage);

        final String jsonStr1 = request.toJsonStr();
        System.out.println("jsonStr1 = " + jsonStr1);

        final Request<ChatMessage> bean = JSONUtil.toBean(jsonStr1, new cn.hutool.core.lang.TypeReference<Request<ChatMessage>>() {
        }, false);

        System.out.println("bean = " + bean);

        final ObjectMapper objectMapper = new ObjectMapper();
        try {
            final Request<ChatMessage> o = objectMapper.readValue(jsonStr1, new TypeReference<>() {
            });
            System.out.println("o = " + o);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }

    }
}